package thread;
class Conter {
    public int count;


    //方法没有加锁，有问题
//    public void addCount(){
//        count++;
//    }
    //synchronize加锁
    synchronized public void addCount(){
        count++;
    }
//    等价于：
//    public void addCount1(){
//        synchronized (this){
//            count++;
//        }
//    }

    //静态方法
//    synchronized public static void  addCount2(){
//
//    }
//    public static void addCount3(){
//        synchronized (Conter.class){
//
//        }
//    }


}
//synchronized 加锁，实例方法
public class demo16 {
    public static void main(String[] args) throws InterruptedException {
        Conter counter = new Conter();
        Thread t1 = new Thread(()->{
            for (int i = 0 ;i<50000;i++){
                counter.addCount();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0 ;i<50000;i++){
                counter.addCount();
            }
        });

        t1.start();

        t2.start();
        t1.join();

        t2.join();

        System.out.println(counter.count);
    }
}
